package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.enums.LabelInfoTypeEnum;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.LabelInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 1
* @description 针对表【label_info(标签信息表)】的数据库操作Service实现
* @createDate 2024-08-28 11:00:26
*/
@Service
public class LabelInfoServiceImpl extends ServiceImpl<LabelInfoMapper, LabelInfo>
    implements LabelInfoService {

    @Override
    public List<LabelInfo> listByType(LabelInfoTypeEnum typeEnum) {
        LambdaQueryWrapper<LabelInfo> queryWrapper = null;
        if(typeEnum!=null){
            queryWrapper = Wrappers.lambdaQuery(LabelInfo.class)
                    .eq(LabelInfo::getType , typeEnum.getCode());
        }
        return this.list(queryWrapper);
    }

    @Override
    public boolean saveOrUpdateLabelInfo(LabelInfo labelInfo) {
        //唯一校验：  type +  name组合唯一
        long id = labelInfo.getId()==null?-1L:labelInfo.getId();
        LabelInfo dbLabelInfo = this.getOne(Wrappers.lambdaQuery(LabelInfo.class)
                .eq(LabelInfo::getType, labelInfo.getType().getCode())
                .eq(LabelInfo::getName, labelInfo.getName()));

        if(dbLabelInfo!=null && id != dbLabelInfo.getId().longValue()){
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }

        return this.saveOrUpdate(labelInfo);
    }
}




